package com.hmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public PageDTO pageQuery(Long page, Long size) {
        IPage page1 = new Page(page, size);
        itemMapper.selectPage(page1, null);
        PageDTO pageDTO = new PageDTO(page1.getTotal(), page1.getRecords());
        return pageDTO;

    }

    @Override
    public Item selectById(Long id) {
        Item item = itemMapper.selectById(id);
        return item;
    }

    @Override
    public void saveproduct(Item item) {
        itemMapper.insert(item);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemMapper.updateById(item);

        Item item1 = itemMapper.selectById(id);
        String s1 = JSON.toJSONString(item1);
        String s="syn.queue";
        rabbitTemplate.convertAndSend(s,s1);
    }

    @Override
    public void updateAll(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    public void deleteById(Long id) {
        itemMapper.deleteById(id);
    }
}
